﻿using System;
using AccountModule;
using Framework;
using UnityEngine;
using UnityEngine.UI;

namespace View
{
    public class RegistView : ViewProxy
    {
        private GameObject _goMales;
        private Image[] _imageMales;
        private GameObject _goFemales;
        private Image[] _imageFemales;

        private InputField _inputName;
        private InputField _inputHeight;
        private InputField _inputWeight;

        private Toggle[] _toggleGenders;
        private Toggle[] _toggleAims;

        private ClickTrigger _triggerConfirm;

        private Account _account;
        
        protected override void SetupView()
        {
            LoadRes("Views/Regist/RegistView");
        }

        protected override void OnInit()
        {
            _account = new Account("", 0, 163, 53, Gender.Male, 0);
            
            _goMales = _transform.Find("MaleIcons").gameObject;
            _imageMales = new Image[3];
            for (int i = 0; i < 3; ++i)
            {
                Image image = _goMales.transform.Find("Icon_" + (i + 1)).GetComponent<Image>();
                _imageMales[i] = image;
            }
            
            _goFemales = _transform.Find("FemaleIcons").gameObject;
            _imageFemales = new Image[3];
            for (int i = 0; i < 3; ++i)
            {
                Image image = _goFemales.transform.Find("Icon_" + (i + 1)).GetComponent<Image>();
                _imageFemales[i] = image;
            }

            _inputName = _transform.Find("Input_Name").GetComponent<InputField>();
            
            _inputHeight = _transform.Find("Input_Height").GetComponent<InputField>();
            _inputHeight.onValueChanged.AddListener(OnHeightChanged);
            
            _inputWeight = _transform.Find("Input_Weight").GetComponent<InputField>();
            _inputWeight.onValueChanged.AddListener(OnWeightChanged);
            
            _toggleGenders = new Toggle[2];
            for (int i = 0; i < 2; ++i)
            {
                Toggle toggle = _transform.Find("Gender/Toggle_" + (i + 1)).GetComponent<Toggle>();
                toggle.onValueChanged.AddListener(OnGenderChanged);
                _toggleGenders[i] = toggle;
            }
            
            _toggleAims = new Toggle[3];
            for (int i = 0; i < 3; ++i)
            {
                Toggle toggle = _transform.Find("Aims/Toggle_" + (i + 1)).GetComponent<Toggle>();
                toggle.onValueChanged.AddListener(OnAimsChanged);
                _toggleAims[i] = toggle;
            }
            
            _triggerConfirm = ClickTrigger.Get(_transform.Find("Button_Confirm"));
            _triggerConfirm.AddClickListener(OnRegistClick);
            
            Update();
        }

        private void Update()
        {
            if (_account.gender == Gender.Male)
            {
                _goMales.SetActive(true);
                _goFemales.SetActive(false);
                for (var i = 0; i < _imageMales.Length; i++)
                {
                    _imageMales[i].gameObject.SetActive(i == (int)_account.aims);
                }
            }
            else
            {
                _goFemales.SetActive(true);
                _goMales.SetActive(false);
                for (var i = 0; i < _imageFemales.Length; i++)
                {
                    _imageFemales[i].gameObject.SetActive(i == (int)_account.aims);
                }
            }

            for (var i = 0; i < _toggleGenders.Length; i++)
            {
                _toggleGenders[i].isOn = i == (int)_account.gender;
            }
            
            for (var i = 0; i < _toggleAims.Length; i++)
            {
                _toggleAims[i].isOn = i == (int) _account.aims;
            }
        }

        private void OnGenderChanged(bool value)
        {
            Gender gender = _account.gender;
            for (var i = 0; i < _toggleGenders.Length; i++)
            {
                if (_toggleGenders[i].isOn)
                {
                    gender = (Gender) i;
                    break;
                }
            }
            _account.gender = gender;
            Update();
        }

        private void OnAimsChanged(bool value)
        {
            Aims aims = _account.aims;
            for (var i = 0; i < _toggleAims.Length; i++)
            {
                if (_toggleAims[i].isOn)
                {
                    aims = (Aims) i;
                    break;
                }
            }
            _account.aims = aims;
            Update();
        }

        private void OnHeightChanged(string value)
        {
            if (!string.IsNullOrEmpty(_inputHeight.text))
            {
                float height = Convert.ToSingle(_inputHeight.text);
                _account.SetHeight(height);
                Update();
            }
        }

        private void OnWeightChanged(string value)
        {
            if (!string.IsNullOrEmpty(_inputWeight.text))
            {
                float weight = Convert.ToSingle(_inputWeight.text);
                _account.SetWight(weight);
                Update(); 
            }
        }

        private void OnRegistClick(params object[] args)
        {
            _account.name = _inputName.text;
            if (!string.IsNullOrEmpty(_account.name))
            {
                Account account = AccountManager.instance.GetAccount(_account.name);
                if (account == null)
                {
                    AccountManager.instance.AddAccount(_account);
                    AccountManager.instance.accountName = _account.name;
                    new MyRecipeView().Show();
                    Close();
                }
            }
        }
    }
}